require.config({
    baseUrl: "./modules",
    paths: {
        jq: "../libs/jquery",
        cookie: "../libs/jquery.cookie"
    },
    shim: {
        cookie: {
            deps: ["jq"]
        }
    }
})
let dataList = [];
require(["fetchData", "cookie"], (fd, _cookie) => {
    const localUrl = "http://localhost:3000";
    // Ajax获取公共的静态页面
    $(".top").load(`/public/publicPage.html #top`);
    $(".bottomBox").load(`/public/publicPage.html #bottomBox`);
    $(".footer").load(`/public/publicPage.html #footer`);


    const url = `${localUrl}/api`;
    // 获取数据
    fd(url, "cart", (res) => {
        console.log("res", res.children);

        // // ttt
        // const dataList = res.forEach(val => {
        //     val.selfSaleBType = false
        // });
        dataList = res;
        console.log(dataList);
        $("#cart").empty();
        // 2. 渲染页面
        renderFn(dataList);

    });




    function renderFn(dataList) {
        let str = "";
        dataList.children.forEach(val => {
            str +=
            `<div class="cartProItem">
                <div class="cartProSelf">
                    <input type="checkbox"  class="selectCart u-chk">
                    <span>
                        <span class="ziying" style="display:${val.selfSaleBType};}">自营</span>
                        <span class="brandName">${val.brandName}</span>
                    </span>
                </div>
                ${ renderChildren(val.children) }
                
            </div>`
        });

    function renderChildren(list){
        var str = ''
        list.map(item=>{
            str +=  `<div class="cartPro">
              <input type="checkbox" ${item.isChecked?'checked':null }  checkFlag=${item.isChecked} data-id=${item.goodsId}
               class="selectCartSelf u-chk">
              <img src=${item.imageUrl} alt="">

              <div style="display: flex;height: 100%;">
                  <div class="cartProInfo txtwrap">
                      <p>${item.title}/p>
                      <p style="margin-top: 5px;">
                          <span class="m-actlabel" style=" margin-top: 5px !important;">
                          特价</span>
                      </p>
                  </div>


                  <div class="skudesc">
                      <p>颜色：Not Specified</p>
                      <p>套餐：29*6*46cm</p>
                  </div>
              </div>

              <div class="newprice">
                  <span>${item.actualCurrentPrice}</span>
                  <a href="">已降1元</a>
              </div>

              <div class="cartNumBox" style="margin-left: 54px;">
                  <div class="flex-c-between">
                      <button class="cartNumAdd">-</button>
                      <input type="number" class="cartNum" value="1" min="1">
                      <button class="cartNumMinus">+</button>
                  </div>
                  <p>每日限购<span>3</span>件</p>
              </div>

              <div class="newprice" style=" margin-left: 65px;">
                  <span>${item.actualCurrentPrice}</span>
              </div>

         
              <div class="newprice2">
                  <span class="u-opt">删除</span>
                  <span class="u-opt">移出我的收藏</span>
              </div>
          </div>`
          })

          return str

    }

     


        $("#cart").html(str);
        addinit(dataList.children);//初始化数据
        addEvent();
    }

    function addinit(dataList){
        dataList.map((item)=>{  //单个商品背景
            item.children.map((opItem,index)=>{
                if(opItem.isChecked){
                       $('.cartPro').eq(index).addClass('cartProSelect')
                }
            })
        })

        checkPrice(dataList)
    }


   function checkPrice(dataList){ // 计算价钱 件数
    var piece = 0 , totalPrice = 0;
    dataList.map((item)=>{ 
        // var itemIsAllSelect =  item.children.some((opItem,index)=>{  //店铺是否都选中背景  true代表里面不是全部选中
        //     if(!opItem.isChecked){
        //         return true
        //     }
        // })

        // if(itemIsAllSelect){
             
        // }
        item.children.map((opItem,index)=>{ // 计算件数和总价 
                if(opItem.isChecked){
                    piece =  piece + 1
                    totalPrice = totalPrice + opItem.actualCurrentPrice* parseInt($('.cartPro').eq(index).find('.cartNum')[0].value)
                }
            })

    })
   
    $('#piece').text(piece)
    $('#totalPrice').text(totalPrice)
   }


    function addEvent(){
        // 点击全选 - 数据（全选 - 全不选）- 元素（全选 - 全不选）
        $(".selectCartAll").on("click",function(){
            dataList.children.forEach(val=>{
                val.isCheckedAllSelf = dataList.isCheckedAll;
                val.children.forEach(val2=>{
                    val2.isChecked = dataList.isCheckedAll;
                })
            })
            // $(".selectCartAll").attr("checked");
            // console.log($(".selectCartAll"));
            console.log(dataList);
        });
        $(".selectCart").on("click",function(){
            // dataList.children.forEach((val,index)=>{
            //     val.children.forEach(val2=>{
            //         val2.isChecked =val.isCheckedAllSelf;
            //     })
            // })
            // $(".selectCartAll").attr("checked");
            // console.log($(".selectCartAll"));

            
            console.log(dataList);
        })

    

        $(".selectCartSelf").on("click",function(){
            const id = $(this).attr("data-id");
            dataList.children.forEach((val,index)=>{
                val.children.forEach((val2,index2)=>{ //商品单个
                    // debugger
                    if (val2.goodsId == id) {
                        val2.isChecked = !val2.isChecked
                        if(val2.isChecked){
                            $(this).parent('.cartPro').addClass('cartProSelect')
                        }
        
                        if(!val2.isChecked){
                            $(this).parent('.cartPro').removeClass('cartProSelect')
        
                        }
                    };
                })
            })
           //改完数据 计算价钱
            checkPrice(dataList.children)
          
        })
    }
})




// 全选

// 店铺全选
// 单选